package com.cloud.common.autoconfig;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.cloud.common.context.*;
import com.cloud.common.controller.IndexController;
import com.cloud.common.exception.GlobalExceptionHandler;
import com.cloud.common.interceptor.ContextInterceptor;
import com.cloud.common.interceptor.FeignInvokeInterceptor;
import com.cloud.common.interceptor.LogDebugInterceptor;
import com.cloud.common.listener.ClosedListener;
import com.cloud.common.listener.StartupListener;
import com.cloud.common.ribbon.RibbonServerSelector;
import com.cloud.common.rpc.ClientHandler;
import com.cloud.common.rpc.RPCClient;
import com.cloud.common.rpc.RPCServer;
import com.cloud.common.rpc.ServerHandler;
import com.cloud.common.swagger.SwaggerDocket;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@Configuration
@Import({AppContext.class,
        MappingContext.class,
        SessionHolder.class,
        VersionHolder.class,
        WebConfigurer.class,
        ContextInterceptor.class,
        StartupListener.class,
        ClosedListener.class,
        LogDebugInterceptor.class,
        FeignInvokeInterceptor.class,
        RibbonServerSelector.class,
        IndexController.class,
        SwaggerDocket.class,
        RPCClient.class,
        RPCServer.class,
        ClientHandler.class,
        ServerHandler.class,
        GlobalExceptionHandler.class,})
public class CommonAutoConfiguration {

    @Bean
    public HttpMessageConverters httpMessageConverters() {
        SerializerFeature[] serializerFeatures = {
                SerializerFeature.WriteDateUseDateFormat,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullStringAsEmpty,
                SerializerFeature.WriteNullListAsEmpty
        };

        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_XML);

        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setSerializerFeatures(serializerFeatures);

        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);

        List<HttpMessageConverter<?>> converters = new ArrayList<>();
        converters.add(fastJsonHttpMessageConverter);
        return new HttpMessageConverters(true, converters);
    }

    @Bean
    public RestTemplate restTemplate() {
        SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
        httpRequestFactory.setConnectTimeout(6000);
        httpRequestFactory.setReadTimeout(30000);

        RestTemplate restTemplate = new RestTemplate(httpRequestFactory);
        restTemplate.setMessageConverters(httpMessageConverters().getConverters());
        return restTemplate;
    }

}
